package com.tuanzi.loan.business.repository.wallet.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanUserWalletFlow.LOAN_USER_WALLET_FLOW;

import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.wallet.custom.LoanUserWalletFlowRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.wallet.LoanUserWalletFlowRequest;
import com.tuanzi.loan.vo.response.wallet.LoanUserWalletFlowResponse;
import com.tuanzi.server.loan_base.util.DateUtils;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

@SuppressWarnings({ "rawtypes" })
@Repository
public class LoanUserWalletFlowRepositoryImpl extends JOOQBaseRepository implements LoanUserWalletFlowRepositoryCustom{
	
	@Override
	protected TableImpl getTable() {
		return LOAN_USER_WALLET_FLOW;
	}

	@Override
	public Page<LoanUserWalletFlowResponse> list(
			LoanUserWalletFlowRequest searchRequest, Pageable pageable) {
		
		fields(LOAN_USER_WALLET_FLOW.fields());
		between(LOAN_USER_WALLET_FLOW.CREATE_TIME, searchRequest.getStartDate(), 
				searchRequest.getEndDate() == null ? searchRequest.getEndDate() : 
					DateUtils.formatDateToYyyyMMdd(searchRequest.getEndDate()) + " 23:59:59");
		
		orderBy(LOAN_USER_WALLET_FLOW.CREATE_TIME, Sort.Direction.DESC);
		
		 return search(pageable, record -> {
			 
			 LoanUserWalletFlowResponse response = new LoanUserWalletFlowResponse();
			 
	            response.setId(record.get(LOAN_USER_WALLET_FLOW.ID));
	            response.setCreateTime(record.get(LOAN_USER_WALLET_FLOW.CREATE_TIME));
	            response.setAmount(record.get(LOAN_USER_WALLET_FLOW.AMOUNT).floatValue());
	            response.setBalance(record.get(LOAN_USER_WALLET_FLOW.BALANCE).floatValue());
	            response.setRemark(record.get(LOAN_USER_WALLET_FLOW.REMARK));
	            response.setWithdrawAccount(PhoneNumNideUtil.hideNum(record.get(LOAN_USER_WALLET_FLOW.WITHDRAW_ACCOUNT)));
	            response.setCreator(record.get(LOAN_USER_WALLET_FLOW.CREATOR));
	            
	            return response;
	        });
		
	}

}
